package com.jiuwu.note.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import com.jiuwu.note.po.HotGoodsPO;

@Repository
public class GoodsDao {
	
	
	@Autowired
	private JdbcTemplate jdbcTemplate ;
	
	public int addGoods(HotGoodsPO goods) {
		
		//String sql = "INSERT INTO t_hot_goods (goods_name,maning_price,add_time) VALUES ( '爱他美【3段】奶粉' , 218 , now()  );";
		String sql = "INSERT INTO t_hot_goods (goods_name,goods_name_en,maning_price,add_time) VALUES ( ? , ? , ? , ? );";
		return jdbcTemplate.update(sql, new Object[] {goods.getGoodName(),goods.getGoodNameEn(),goods.getManingPrice(),new Date()});
	}
	

	
	public List<HotGoodsPO> getAllGoods(){
		String sql = "SELECT T.goods_name,T.goods_name_en,T.maning_price,T.add_time FROM t_hot_goods AS T;";
		final List<HotGoodsPO> list = new ArrayList<>();
		RowMapper<HotGoodsPO> rowMapper = new RowMapper<HotGoodsPO>() {
			
			@Override
			public HotGoodsPO mapRow(ResultSet rs, int rowNum) throws SQLException {
				HotGoodsPO po = new HotGoodsPO();
				po.setAddTime(rs.getDate("add_time"));
				po.setGoodName(rs.getString("goods_name"));
				po.setGoodNameEn(rs.getString("goods_name_en"));
				po.setManingPrice(rs.getFloat("maning_price"));
				list.add(po);
				return po;
			}
		};
		
		jdbcTemplate.query(sql, new Object[] {}, rowMapper);
		return list;
	}
	
	
}
